/**
 * 
 */
package org.pump.client.codec;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.mina.core.buffer.IoBuffer;
import org.pump.client.type.LengthCodedBinary;
import org.pump.client.type.RowValue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Statement;

/**
 * @author liudong03
 *
 */
public class DeleteRowEvent extends EventData{
	private static final Logger mylog = LoggerFactory
			.getLogger(DeleteRowEvent.class);
	long table_id ;
	byte[] flags = new byte[2];
	LengthCodedBinary column_count;
	boolean[] used_column;
	List<RowValue> rowvalue;
	int length;
	
	public boolean getSql(List<String> colname,String deletestat,Statement st)
	{
		int sz = this.rowvalue.size();
		for(int i = 0; i < sz; i++)
		{
			StringBuilder sb = new StringBuilder();
			sb.append(deletestat);
			RowValue currow = this.rowvalue.get(i);
			List<Integer> rowpos = currow.getColumn_pos();
			List<Object> rowvalue = currow.getColumn_value();
			int colcount = this.used_column.length;
			int j = 0;
			int valuesz = rowvalue.size();
			for(int k = 0; k < valuesz; k++)
			{
				for(;j < colcount; j++)
				{
					if(rowpos.get(k).intValue() == j)
					{
						sb.append(colname.get(j));
						sb.append("=");
						sb.append(rowvalue.get(k));
						
						if(k != valuesz -1) sb.append(" and ");
						j++;
						break;
					}
				}
			}
			
			try {
				mylog.info(Thread.currentThread().getName() + " delete sql=" + sb.toString());
				st.addBatch(sb.toString());
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				mylog.error("can't add batch",e);
				return false;
			}
		}
		return true;
	}
	public static DeleteRowEvent readBuffer(IoBuffer in,Packet prevpacket,int len)
	{
		if(in.remaining() < len)
		{
			return null;
		}
		
		DeleteRowEvent myevent = new DeleteRowEvent();
		long temp = 0;
		for(int i = 0; i < 6; i++)
		{
			long curbyte = in.getUnsigned();
			temp = curbyte << (i *8);
			myevent.table_id += temp;
		}
		myevent.length += 6;
		
		in.get(myevent.flags);
		myevent.length += 2;
		
		myevent.column_count = LengthCodedBinary.readBuffer(in);
		myevent.length += myevent.column_count.getLen();
		
		long colcount = myevent.column_count.getData();
		
		long sz =( colcount + 7) >> 3;
		byte[] curtmp = new byte[(int) sz];
		in.get(curtmp);
		myevent.length += sz;
		
		myevent.used_column = new boolean[(int) colcount];
		for(int i = 0; i < colcount; i++)
		{
			myevent.used_column [i] = ((curtmp[i/8] & (0x01 << (i%8))) == 0)?false:true;
		}
		
		int remainbytes = len - myevent.length;
		while(remainbytes > 0)
		{
			RowValue currowvalue = RowValue.readBuffer(in, len - myevent.length, prevpacket, myevent.used_column);
			if(currowvalue == null)
			{
				mylog.error("read row value fail");
				return null;
			}
			myevent.rowvalue.add(currowvalue);
			myevent.length += currowvalue.getRowlen();
			remainbytes = len - myevent.length;
		}
		
		return myevent;
	}
	/**
	 * 
	 */
	public DeleteRowEvent() {
		// TODO Auto-generated constructor stub
		this.rowvalue = new ArrayList<RowValue>();
	}
	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "DeleteRowEvent [table_id=" + table_id + ", flags="
				+ Arrays.toString(flags) + ", column_count=" + column_count
				+ ", used_column=" + Arrays.toString(used_column)
				+ ", rowvalue=" + rowvalue + ", length=" + length + "]";
	}
	/**
	 * @return the table_id
	 */
	public long getTable_id() {
		return table_id;
	}
	/**
	 * @return the rowvalue
	 */
	public List<RowValue> getRowvalue() {
		return rowvalue;
	}
}
